<?php
if($_SESSION['sid']=="")
{
    fs_showmsg('您还没有登录!','?do=index');
}

$action=fs_get_value("ctr");
switch($action)
{
	case    "test"  :   test()  ;   break;
    case    "calculate_point" :get_result();break;
	default			:	main()	;	break;
}
?>
<?php
//试题MAIN
function main(){
	global $db;
	$test_id = intval(fs_get_value("id"));
    $ch_sql = "select * from ".DBPREFIX."answer where exam_id=".$test_id ." and uid=".$_SESSION['sid'];
    $query = $db->query($ch_sql);
    $num = mysql_num_rows($query);
    /*if($num>0)
    {
        fs_showmsg('您已经参加过该项考试,不能重复参加!','?do=exam');
    }*/
?>
<!--<script language="JavaScript">
var maxtime = <?php echo get_test_info($test_id,'exam_time')?>*60;
function CountDown(){
    if(maxtime>=0){  
        minutes = Math.floor(maxtime/60);  
        seconds = Math.floor(maxtime%60);  
        msg = "<font color='blue'>"+minutes+"分"+seconds+"秒</font>"; 
        $("#timeleft").html(msg);  
        if(maxtime == 5*60)
        {
            alert('注意，还有5分钟!');  
        }
        --maxtime;
    }
    else{
        clearInterval(timer);  
        alert("时间到,考试结束!");
        $("#exam").submit();
        
    }  
}
timer = setInterval("CountDown()",1000); 
</script>-->
<div class="splist_content">
    <div class="splist_content_left_test">
		<div class="splist_content_left_content_test">
		    <div class="test">
				<!--testhead-->
				<div class="test_head">
					<div class="test_head_left"></div>
					<div class="test_head_center"></div>
					<div class="test_head_right"></div>
				</div>
				
				<div class="test_title">
					<?php echo(get_test_info($test_id,'exam_name'));?>
				</div>
				<div class="test_info">
					考试时间：<?php echo(get_test_info($test_id,'exam_time'));?>&nbsp;&nbsp;&nbsp;&nbsp;
                    已经有 <font color="#FF9933"><?php echo(get_test_info($test_id,'exam_num'));?></font> 人做过此试卷&nbsp;&nbsp;&nbsp;&nbsp;
                    试卷总分:<font color="#FF9933"><?php echo(get_test_info($test_id,'exam_score'));?></font>
                    剩余时间:<span id="timeleft" style="font-size:16px;"></span>
				</div>
			
				<!--题目列表开始-->
				<div class="test_body">
				 <form action="?do=examinfo&ctr=calculate_point&test_id=<?php echo($test_id);?>" method="post"  id="exam">
					<?php
						$sql = "select * from ".DBPREFIX."exam_thread where exam_id=".$test_id." order by thread_sort ASC";
						$result=$db->query($sql);
						$i=0;
						while($row= $db->fetch_array($result)){
                            switch($row['thread_type'])
                            {
                                case 1:
                                    $row['type']='单项选择题';
                                    echo "<input type='hidden' value=".$row['perscore']." name='score1' />";
                                    break;
                                case 2:
                                    $row['type']='多项选择题';
                                    echo "<input type='hidden' value=".$row['perscore']." name='score2' />";
                                    break;
                                case 3:
                                    $row['type']='判断题';
                                    echo "<input type='hidden' value=".$row['perscore']." name='score3' />";
                                    break;
                                default:$row['type']='';
                            }
		?> 		
					<div class="test_body_part">
						<B><img src="resources/images/heading2-bullet.png" height="10" /> <?php echo $row['type'] ?>(<?php echo $row['thread_des'] ?>)</B>
					</div>
							<?php
								$result_question =$db->query("select a.*,b.* from ".DBPREFIX."examconnect as a inner join ".DBPREFIX."question as b on b.q_id=a.q_id where a.thread_id=".$row['thread_id']." order by b.q_sort desc");
                                $num = $db -> num_rows($result_question);
								while($row_question = $db -> fetch_array($result_question)){
                                    $options = explode('|',$row_question['q_option']);
                                    $option_str='';
                                    foreach($options as $key=>$op)
                                    {
                                        $delete_p_op = str_replace('<p>','',$op);
                                        $delete_p_op = str_replace('</p>','',$delete_p_op);
                                        $option_s = array('A','B','C','D','E','F','G','H','I','G');
                                        $option_str .=$option_s[$key].":".$delete_p_op."<br/>";
                                    }
									$i++;
                                    $row_question['q_title'] =str_replace('<p>','',$row_question['q_title']);
                                    $row_question['q_title'] =str_replace('</p>','',$row_question['q_title']);//给标题二次删除p
					?>
				   
					<!--item-->		
					<div class="test_body_item">
						<div class="test_body_item_question">
							   第<font style="color:#FF0000;"><?php echo($i);?></font>题&nbsp&nbsp<?php echo $row_question['q_title']?>
						</div>
						<div class="test_body_item_answer">
							  <?php echo $option_str ?>
						</div>
					
						<div class="test_body_item_checkbox" style="line-height:21px;">
							请选择答案： <?php
											  switch($row_question['q_type']){//
												  case 1://1为单选?>
												  <input type="radio" name="answer_radio<?php echo($row_question['q_id']);?>" value="A" /> A 
												  <input type="radio" name="answer_radio<?php echo($row_question['q_id']);?>" value="B" /> B  
												  <input type="radio" name="answer_radio<?php echo($row_question['q_id']);?>" value="C" /> C 
												  <input type="radio" name="answer_radio<?php echo($row_question['q_id']);?>" value="D" /> D 
                                                  <input type="hidden" value="<?php echo 's_'.$row_question['q_id']?>" name="qid[]"/>
												  <?php ; break;
												  case 2:
                                                  $abc = array('A','B','C','D','E','F');
                                                  for($f=0;$f<count($options);$f++){
                                                     echo '<input type="checkbox" name="more_options'.$row_question["q_id"].'[]" value="'.$abc[$f].'" />'.$abc[$f]; 
                                                  }
//2为多选?>
                                                  <input type="hidden" value="<?php echo 'm_'.$row_question['q_id']?>" name="qid[]"/>
												  <?php ; break;
												  case 3://3为判断?>
												  <input type="radio" name="answer_judge<?php echo($row_question['q_id']);?>" value="A" /> A 
												  <input type="radio" name="answer_judge<?php echo($row_question['q_id']);?>"value="B" /> B 
                                                  <input type="hidden" value="<?php echo 'j_'.$row_question['q_id']?>" name="qid[]"/>
												  <?php ; break;
			}
			?>
						</div>
					</div>
					<!--end-->
					<?php
						}
					  }
					?>
					<div style="width:45%;text-align:right;padding:10px; float:left;"><input type="submit" value="提交答案" /> <input type="button" value="返回上一页" onclick="javascript:history.go(-1)" /></div>
		</form>
				</div>
				<!--end-->
			</div>
			</div>
		</div>
</div>
<?php
}	

//提交答案
function get_result(){
	global $db;
    $_SESSION['timer']=0;
	$test_id = intval(fs_get_value('test_id'));
	$sql = "update ".DBPREFIX."exam set exam_num = exam_num + 1 where exam_id = ".$test_id;//做完试卷 试卷+1
	$db -> query($sql);
    $insert_sql = "insert into ".DBPREFIX."record (`uid`,`cid`,`datetime`,`type`) values(".$_SESSION['sid'].",".$test_id.",".time().",3)";//添加试题记录
    $db->query ($insert_sql);
    $qid  =$_POST['qid'];
    
    foreach($qid as $value)
    {
        if(strpos($value,'s_')!==false)
        {    
            $id = substr($value,2);
            $var = 'answer_radio'.$id;
            $answer = $_POST[$var];
            
            $an_sql = "select q.q_answer from ".DBPREFIX."question q where q_id=".$id;
            $an_result =$db->query($an_sql);
            $an_answer = $db->fetch_array($an_result);
            //print_r($_POST['score1']);exit();
            if($answer == $an_answer['q_answer'])
            {
                $score = $_POST['score1'];
            }
            else
            {
                $score = 0;
            }
            
        }
        elseif(strpos($value,'m_')!==false)
        {    
            $id = substr($value,2);
            $var = 'more_options'.$id;
            $answer = $_POST[$var];
            $answer = implode(',',$answer);
            $an_sql = "select q.q_answer from ".DBPREFIX."question q where q_id=".$id;
            $an_result =$db->query($an_sql);
            $an_answer = $db->fetch_array($an_result);
            if($answer == $an_answer['q_answer'])
            {
                $score = $_POST['score2'];
            }
            else
            {
                $score = 0;
            }
            
        }
        elseif(strpos($value,'j_')!==false)
        {          
            $id = substr($value,2);
            $var = 'answer_judge'.$id;
            $answer = $_POST[$var];
            $an_sql = "select q.q_answer from ".DBPREFIX."question q where q_id=".$id;
            $an_result =$db->query($an_sql);
            $an_answer = $db->fetch_array($an_result);
            if($answer == $an_answer['q_answer'])
            {
                $score = $_POST['score3'];
            }
            else
            {
                $score = 0;
            }
            
        }
 
        //$q_sql = "select * from ".DBPREFIX."question as a inner join ".DBPREFIX."examconnect as b on a.q_id=b.q_id inner join ".DBPREFIX."exam_thread as c on b.thread_id=c.thread_id where a.q_id=".$id;
        $answer_sql = "insert into ".DBPREFIX."answer (`exam_id`,`question_id`,`answer`,`uid`,`score`,`exam_time`) values(".$test_id.",".$id.",'".$answer."',".$_SESSION['sid'].",".$score.",".time().".)";
        $query = $db->query($answer_sql);
    }
    if($query)
    {
        fs_showmsg('试卷提交成功!','?do=exam');//,'?do=examlist&tid='.$_GET['test_id']
    }
    
?>

<?php
}
?>